package com.tsyun.view_project.text;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.tsyun.view_project.R;

public class SpannableActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spannable);
        TextView tvContent = findViewById(R.id.tv_content);
        SpannableString spannableString = new SpannableString("this is a color span!!!");
        //创建一个前景色对象 文字颜色
//        ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
//        spannableString.setSpan(span,0,3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
////        tvContent.setText(spannableString);
//        //修改文字的背景颜色
//        BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.parseColor("#4344d0"));
//        spannableString.setSpan(backgroundColorSpan,2,6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //修改部分字体大小
        //绝对的字体大小
        //如果后面不加true，则默认单位为px（像素）
        AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(20,true);
        spannableString.setSpan(absoluteSizeSpan,10,15, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
//        tvContent.setMovementMethod(LinkMovementMethod.getInstance());
        //相对（当前的）字体大小
        RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(1.2f);
        spannableString.setSpan(relativeSizeSpan,1,7,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        tvContent.setText(spannableString);
        //可点击span
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Toast.makeText(SpannableActivity.this,"点击了我",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                //设置链接文本颜色
                ds.setColor(Color.BLACK);
                //设置文本是否显示下划线
//                ds.setUnderlineText(false);
//                ds.linkColor = Color.TRANSPARENT;
                //下划线颜色
//                ds.underlineColor
                //下划线的粗细
//                ds.underlineThickness
            }
        };
        spannableString.setSpan(clickableSpan,10,15, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        //设置文本为可点击的链接的样式
        //设置文本高亮颜色为透明，则点击链接时，会没有阴影
        tvContent.setHighlightColor(Color.TRANSPARENT);
        tvContent.setMovementMethod(LinkMovementMethod.getInstance());
        //文字加下划线
        UnderlineSpan underlineSpan = new UnderlineSpan();
        //更新下划线的状态 仅在Android29以上有效
        TextPaint textPaint = new TextPaint();
//        textPaint.underlineColor = Color.parseColor("#567812");
        underlineSpan.updateDrawState(textPaint);
        spannableString.setSpan(underlineSpan,0,15, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        //不可用，Android P以上可用 Typeface.MONOSPACE  MONOSPACE 等宽字体
//        TypefaceSpan typefaceSpan = new TypefaceSpan(Typeface.DEFAULT_BOLD);
//        spannableString.setSpan(typefaceSpan,0,8,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvContent.setText(spannableString);
        //horizontal layout_gravity
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}